package com.sctvcloud.renhe.application;

import android.app.Activity;
import android.app.Notification;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Bundle;
import android.support.multidex.MultiDexApplication;
import android.util.Log;

import com.gridsum.tracker.GridsumWebDissector;
import com.iflytek.cloud.SpeechUtility;
import com.jiongbull.jlog.JLog;
import com.mob.MobSDK;
import com.mob.tools.utils.DeviceHelper;
import com.ruihang.generalibrary.utils.DatasVersionManager;
import com.ruihang.generalibrary.utils.GeneraLifeObser;
import com.sctvcloud.renhe.R;
import com.sctvcloud.renhe.ui.activities.AdBlankActivity;
import com.sctvcloud.renhe.ui.activities.SplashActivity;
import com.sctvcloud.renhe.ui.utils.SkipUtil;
import com.sctvcloud.renhe.ui.utils.UIUtils;
import com.sctvcloud.renhe.utils.LocationUtils;
import com.shuyu.gsyvideoplayer.utils.Debuger;
import com.tencent.bugly.crashreport.CrashReport;
import com.tencent.smtt.sdk.QbSdk;

import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

import cn.jpush.android.api.BasicPushNotificationBuilder;
import cn.jpush.android.api.JPushInterface;

/**
 * Created by Administrator on 2017/6/15.
 */

public class SiChuanFocusApplication extends MultiDexApplication {
    public static List<Integer> notificationList = new ArrayList<>();
    private DatasVersionManager datasVersionManager;
    public int count = 0;
    private long backTime = 0;
    private long disTime = 3 * 60 * 1000;

    @Override
    public void onCreate() {
        super.onCreate();
        GeneraLifeObser.getInstance().changeLifeState("SiChuanFocusApplication", GeneraLifeObser.STATE_CREATED);
        UIUtils.initDisplayMetrics(this);
        CrashReport.initCrashReport(getApplicationContext(), "739e222ad1", true);

        MobSDK.init(getApplicationContext(), "2b75b8742219a", "734e4cba0adfb165c49c77362e0f028b");

        try {
            ApplicationInfo info = getApplicationInfo();
            if ((info.flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0) {
                Debuger.enable();
                Constances.IS_TESTING = true;
            } else {
                Debuger.disable();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        JLog.init();
        x.Ext.init(this);
        x.Ext.setDebug(true);

        JPushInterface.setDebugMode(true);    // 设置开启日志,发布时请关闭日志
        JPushInterface.init(this);
        Log.e("Sic", JPushInterface.getRegistrationID(this));
        BasicPushNotificationBuilder builder = new BasicPushNotificationBuilder(this);
        builder.statusBarDrawable = R.mipmap.ic_launcher;
        builder.notificationFlags = Notification.FLAG_AUTO_CANCEL
                | Notification.FLAG_SHOW_LIGHTS;  //设置为自动消失和呼吸灯闪烁
        builder.notificationDefaults = Notification.DEFAULT_SOUND
                | Notification.DEFAULT_VIBRATE
                | Notification.DEFAULT_LIGHTS;  // 设置为铃声、震动、呼吸灯闪烁都要
        JPushInterface.setPushNotificationBuilder(3, builder);

        //搜集本地tbs内核信息并上报服务器，服务器返回结果决定使用哪个内核。

        QbSdk.PreInitCallback cb = new QbSdk.PreInitCallback() {

            @Override
            public void onViewInitFinished(boolean arg0) {
                //x5內核初始化完成的回调，为true表示x5内核加载成功，否则表示x5内核加载失败，会自动切换到系统内核。
            }

            @Override
            public void onCoreInitFinished() {
            }
        };
        //x5内核初始化接口
        QbSdk.initX5Environment(getApplicationContext(), cb);

        SpeechUtility.createUtility(this, "appid=" + getString(R.string.voice_id));

        GridsumWebDissector.getInstance().setApplication(this);

        registerActivityLifecycleCallbacks(new ActivityLifecycleCallbacks() {

            @Override
            public void onActivityStopped(Activity activity) {

                count--;
                if (count == 0) {
                    //退到后台
                    SkipUtil.skipToAddUserOperate(SiChuanFocusApplication.this, "SiChuanFocusApplication",  23, 0, "退到后台", "", "" );
                    backTime = System.currentTimeMillis();
                }
            }

            @Override
            public void onActivityStarted(Activity activity) {
                if (count == 0) {
                    //进入前台
                    SkipUtil.skipToAddUserOperate(SiChuanFocusApplication.this, "SiChuanFocusApplication",  22, 0, "进入前台", "", "" );
                    long time = System.currentTimeMillis();
                    if (time - backTime >= disTime) {
                        if (!(activity instanceof SplashActivity)) {
                            Intent intent = new Intent(SiChuanFocusApplication.this, SplashActivity.class);
                            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                            intent.putExtra("FROM_TAG", true);
                            startActivity(intent);
                            activity.overridePendingTransition(0, 0);
                        }
                    }
                }
                count++;
            }

            @Override
            public void onActivitySaveInstanceState(Activity activity, Bundle outState) {
            }

            @Override
            public void onActivityResumed(Activity activity) {
            }

            @Override
            public void onActivityPaused(Activity activity) {
            }

            @Override
            public void onActivityDestroyed(Activity activity) {
            }

            @Override
            public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
            }
        });

    }

    @Override
    public Resources getResources() {
        //重写此方法，屏蔽系统设置字体大小对当前APP字体的影响
        Resources res = super.getResources();
        Configuration config = new Configuration();
        config.setToDefaults();
        res.updateConfiguration(config, res.getDisplayMetrics());
        return res;
    }

    public synchronized DatasVersionManager getDatasVersionManager() {
        if (datasVersionManager == null) {
            datasVersionManager = DatasVersionManager.getInstance();
        }
        return datasVersionManager;
    }
}
